package dao

import "encoding/json"

type Permission struct {
	BaseModel
	Code        string
	Method      string
	Name        string
	Description string
	Application string
	GroupCode   string
	ParentId    int
	MenuCode    string
	MenuName    string
}

func (c *Permission) TableName() string {
	return "permission"
}

func (c *Permission) ToJsonStr() string {
	marshal, err := json.Marshal(c)
	if err != nil {
		return ""
	}
	return string(marshal)
}

func SelectAllPermission() *[]Permission {
	permission := make([]Permission, 0)
	DB.Where("is_del = ?", 0).Find(&permission)
	return &permission
}

func SelectPermissionByExample(permission *Permission) *[]Permission {
	permissions := make([]Permission, 0)
	DB.Where(&permission)
	DB.Find(&permissions)
	return &permissions
}

func SavePermission(permission *Permission) int {
	DB.Create(&permission)
	return permission.ID
}

func SavePermissionBatch(permissions *[]Permission) {
	for _, permission := range *permissions {
		DB.Create(&permission)
	}
}

func UpdatePermission(permission *Permission) {
	DB.Model(&permission).Save(&permission)
}

func DeletePermission(id int) {
	permission := Permission{}
	permission.ID = id
	DB.Delete(permission)
}

func DeletePermissionByExample(permission *Permission) {
	DB.Where(&permission).Delete(Permission{})
}
